
var app = getApp();
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['全部活动', '正在进行', '已结束','活动预告'],
    currentTab: 0,
    pn: 1,
    type: "",
    buttonClicked: false,
    activities:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("-----------------");
    this.getActive("");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getActive(this.data.type,this.data.cateId);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    let url = util.getCurrentPageUrlWithArgs();
    if(url.indexOf('?') > -1){
      url += '&fid='+app.globalData.fid+'&fidEnc='+app.globalData.fidEnc;
    }else{
      url += '?fid='+app.globalData.fid+'&fidEnc='+app.globalData.fidEnc;
    }
    if(options.from == "menu"){
      return {
        title:app.globalData.site.name,
        path: url
      }
    }
  },

  navbarTap: function(e){
    // this.setData({
    //   currentTab: e.currentTarget.dataset.idx
    // })

    this.setData({
      currentTab: e.currentTarget.dataset.idx,
      pn: 1,
      activities: [],
      isOver: false
    })
    var index = e.currentTarget.dataset.idx;
    //根据索引值判断type参数值
    if(index == 0){
      this.setData({
        type: ""
      })
    }else if (index == 1){
      this.setData({
        type: "ing"
      })
    } else if (index == 2) {
      this.setData({
        type: "-1"
      })
    } else if (index == 3) {
      this.setData({
        type: "1"
      })
    }
    this.getActive(this.data.type);

  },
  gotoDetail: function(e){
    let index = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './../activityDetail/activityDetail?id='+index,
    })
  },

  /**
   * 获取活动
   */
  getActive: function(){
    var that = this;
    wx.request({
      url: app.globalData.activityHost + '/services/activityaction/listActivityByCategory',
      data: {
        fid: app.globalData.fid,
        type: that.data.type,
        currentPage: that.data.pn,
        pageSize: 3
      },
      success: function (res) {
        console.log(res);
        if(res.data.result == "0"){
          if(res.data.obj.data.length > 0){
            that.setData({
              activities: that.data.activities.concat(res.data.obj.data),
              pn: that.data.pn + 1
            })
          }else{
            console.log("暂无");
            that.setData({
              isOver: true
            })
          }          
        }else{
          console.log("获取活动失败");
        } 

      },
      fail: function () {
        console.log("获取分类失败");
      }
    })
  }

})